package symbolTable.primaryST;


import stackAndQueue.Queue;

public class SequentialSearchST<Key, Value> {
    private Node first;//初始值为null

    private class Node {
        Key key;
        Value value;
        Node next;

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    public Value get(Key key) {
        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key))
                return x.value;
        }
        return null;
    }

    public void put(Key key, Value value) {
        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key)){
                x.value = value;
                return;
            }

        }
        first = new Node(key, value, first);
    }

    public int size() {
        int num = 0;
        for (Node x = first; x != null; x = x.next) {
            num++;
        }
        return num;
    }

    public void delete(Key key) {
        Node x_prior = new Node(null, null, first);
        for (Node x = first; x != null; x = x.next) {
            if (x.key == key){
                x_prior.next = x.next;
                return;
            }
            //记录前一个节点的信息
            x_prior = x;
        }
    }
    public Iterable<Key> keys(){
        Queue<Key> queue = new Queue<>();
        for (Node x = first; x != null; x = x.next){
            queue.enqueue(x.key);
        }
        return queue;
    }
}
